package com.jic.product.impl;

import java.lang.Long;
import java.util.List;

import com.jic.common.base.vo.RestResult;
import com.jic.product.entity.ProductPropertyNameRelation;
import com.jic.product.entity.ProductPropertyValueRelation;
import com.jic.product.mapper.ProductPropertyNameRelationMapper;
import com.jic.product.mapper.ProductPropertyValueRelationMapper;
import com.jic.product.manage.request.ProductParentRequest;
import com.jic.product.manage.request.ProductPropertyRequest;
import com.jic.product.service.ProductPropertyNameRelationService;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 商品属性名
 *
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-01-10 07:03:01
 */
@Service
public class ProductPropertyNameRelationServiceImpl implements ProductPropertyNameRelationService {

    @Autowired
    private ProductPropertyNameRelationMapper productPropertyNameRelationMapper;

    @Autowired
    private ProductPropertyValueRelationMapper productPropertyValueRelationMapper;

    @Override
    public long insert(ProductPropertyNameRelation productPropertyNameRelation) {
        long flag = productPropertyNameRelationMapper.insert(productPropertyNameRelation);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return productPropertyNameRelationMapper.delete(id);
    }

    @Override
    public long update(ProductPropertyNameRelation productPropertyNameRelation) {
        return productPropertyNameRelationMapper.update(productPropertyNameRelation);
    }

    @Override
    public ProductPropertyNameRelation queryByPrimaryKey(Long id) {
        return productPropertyNameRelationMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<ProductPropertyNameRelation> query(ProductPropertyNameRelation productPropertyNameRelation) {
        return productPropertyNameRelationMapper.query(productPropertyNameRelation);
    }

    @Override
    public PageResult<ProductPropertyNameRelation> queryPage(ProductPropertyNameRelation productPropertyNameRelation, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<ProductPropertyNameRelation> list = productPropertyNameRelationMapper.queryPage(startIndex, pageQuery.getLimit(), productPropertyNameRelation);
        long count = productPropertyNameRelationMapper.queryPageCount(productPropertyNameRelation);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public RestResult<Boolean> savePropertyNameRelation(ProductParentRequest productParentRequest) {
        //先删除所有该商品的属性名属性值
        productPropertyNameRelationMapper.deleteProductPropertyNameRelation(productParentRequest.getProductId());
        for (ProductPropertyRequest productPropertyRequest : productParentRequest.getProductPropertyRequest()) {
            //保存属性名关联表
            ProductPropertyNameRelation productPropertyNameRelation = new ProductPropertyNameRelation();
            productPropertyNameRelation.setProductId(productParentRequest.getProductId());
            productPropertyNameRelation.setAttNameId(productPropertyRequest.getAttrId());
            productPropertyNameRelationMapper.insert(productPropertyNameRelation);
            //保存属性值关联表
            ProductPropertyValueRelation productPropertyValueRelation = new ProductPropertyValueRelation();
            productPropertyValueRelation.setAttValueId(productPropertyRequest.getValueId());
            productPropertyValueRelation.setProductAttNameId(productPropertyNameRelation.getId());
            productPropertyValueRelationMapper.insert(productPropertyValueRelation);
        }
        return RestResult.success(true);
    }
}